<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>socket demo</title>
</head>
<body>

</body>
</html>

<script>
    // 创建 WebSocket 实例，连接到服务器地址
    const socket = new WebSocket('ws://localhost:8080');

    // 监听连接成功事件
    socket.addEventListener('open', (event) => {
        console.log('Connected to the server');
        // 向服务器发送消息
        socket.send('Hello, server!');
    });

    // 监听接收到服务器消息事件
    socket.addEventListener('message', (event) => {
        console.log('Received message from server:', event.data);
    });

    // 监听连接关闭事件
    socket.addEventListener('close', (event) => {
        console.log('Connection closed');
    });

    // 监听错误事件
    socket.addEventListener('error', (event) => {
        console.error('WebSocket error:', event);
    });
</script>

